package org.douxiaoban.client.widget;

import org.douxiaoban.client.app.Assistant;

import android.content.Context;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.TranslateAnimation;
import android.widget.ListView;

public class ElasticListView extends ListView{
	private Rect mNormal = new Rect();
	private float mTempY;
	
	private ParentView mParent;

	public ElasticListView(Context context) {
		super(context);
	}
	
    public ElasticListView(Context context, AttributeSet attrs) {  
        super(context, attrs);  
    }
	
    @Override
    public boolean onTouchEvent(MotionEvent ev){
    	customTouchEvent(ev);
    	return super.onTouchEvent(ev);
    }
    
    private void customTouchEvent(MotionEvent ev){
    	switch(ev.getAction()){
    	case MotionEvent.ACTION_DOWN:
    		mTempY = ev.getY();
    		break;
    	case MotionEvent.ACTION_UP:
    		if(isNeedAnimation()){
    			animation();
    		}
    	case MotionEvent.ACTION_MOVE:
    		View view = (View)mParent;
            final float preY = mTempY;  
            float nowY = ev.getY();  
            int deltaY = (int) (preY - nowY);  
            Log.v("preY is",String.valueOf(preY));
            Log.v("nowy is",String.valueOf(nowY));
            Log.v("deltaY is",String.valueOf(deltaY));
            // 滚动  
            scrollBy(0, deltaY);  
            mTempY = nowY;  
            // 当滚动到最上或者最下时就不会再滚动,移动父布局！
            if (isNeedMove()){
            Log.v("need move", "needMove");
            	if(mNormal.isEmpty()){
            		mNormal.set(view.getLeft(),
	            				view.getTop(),
	            				view.getRight(),
	            				view.getBottom());
            	}
//            	view.layout(view.getLeft(),
//            				view.getTop()-deltaY, 
//            				view.getRight(), 
//            				view.getBottom() - deltaY);
            	view.scrollBy(0, -deltaY);
            	int height = this.getHeight();
            	if(view.getTop() - mNormal.top > height/5){
            		mParent.changeStatus(Assistant.NEED_UPDATE);
            	}else{
            		mParent.changeStatus(Assistant.UN_NEED_UPDATE);
            	}
            }
    	}
    }
    
    //针对listView
    private boolean isNeedAnimation(){
    	return !mNormal.isEmpty();
    }
    
    private void animation(){
    	View view = (View)mParent;
    	TranslateAnimation ta 
    			= new TranslateAnimation(0, 0, view.getTop(),mNormal.top);
    	ta.setDuration(200);
    	startAnimation(ta);
    	view.layout(mNormal.left, mNormal.top, mNormal.right, mNormal.bottom);
    	mNormal.setEmpty();
    }
//    //针对箭头
//    private boolean isNeedRotAnimation(){
//    	
//    }
    public void setParent(ParentView parent){
    	mParent = parent;
    }
    public interface ParentView{
    	void changeStatus(int status);
    }
    
    private boolean isNeedMove(){
    	
    	return 0 == getScrollY();	
    }
    
    
}